/*
 * ShopCategoryGoodsPresenter.java
 * business
 *
 * Created by ChenTao on 2016/10/25.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.shopdetail.categorygoods;

import android.os.Bundle;

import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.model.IShopModel;
import com.yidiandao.business.data.recyclertype.DoubleGoods;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.List;

/** Presenter */
public class ShopCategoryGoodsPresenter implements ShopCategoryGoodsContract.Presenter {

    private ShopCategoryGoodsContract.View mShopCategoryGoodsView;

    private IShopModel mShopModel;

    private String categoryId;

    private String supplierId;

    private boolean firstIn = true;

    private List<Goods> mGoodsList;

    private List<DoubleGoods> mGoodsGridList;

    private boolean mEmptyResult;

    private boolean mHasMoreData;

    private boolean isGridStyle;

    public ShopCategoryGoodsPresenter(ShopCategoryGoodsContract.View view, IShopModel shopModel) {
        mShopCategoryGoodsView = view;
        mShopModel = shopModel;

        mShopCategoryGoodsView.setPresenter(this);
    }

    @Override
    public void start() {
    }

    @Override
    public void extraBundle(Bundle bundle) {
        supplierId = bundle.getString(Intents.EXTRA_SUPPLIER_ID);
        categoryId = bundle.getString(Intents.EXTRA_CATEGORY_ID);
        mShopCategoryGoodsView.setTitle(bundle.getString(Intents.EXTRA_CATEGORY_NAME));
    }

    @Override
    public void loadCategoryNewGoodsList(final boolean isRefreshing) {

        if (isRefreshing) {
            if (!firstIn) {
                mShopCategoryGoodsView.showLoadingDialog();
            }
        }

        mShopModel.loadCategoryNewGoodsList(
                isRefreshing,
                supplierId,
                categoryId,
                mShopCategoryGoodsView.getPagerTag(),
                new IShopModel.OnLoadShopGoodsListener() {
                    @Override
                    public void onSuccess(
                            List<Goods> goodsList,
                            List<DoubleGoods> goodsGridList,
                            boolean emptyResult,
                            boolean hasMoreData) {
                        handleResponse(
                                goodsList, goodsGridList, emptyResult, hasMoreData, isRefreshing);
                    }

                    @Override
                    public void onFailed() {
                        if (isRefreshing) {
                            mShopCategoryGoodsView.refreshContent(
                                    SuperLoadingLayout.STATE_ERROR);
                        } else {
                            mShopCategoryGoodsView.loadMoreError();
                        }
                    }

                    @Override
                    public void onFinal() {
                        firstIn = true;
                        mShopCategoryGoodsView.hideLoadingDialog();
                    }
                });
    }

    private void handleResponse(
            List<Goods> goodsList,
            List<DoubleGoods> goodsGridList,
            boolean emptyResult,
            boolean hasMoreData,
            boolean isRefreshing) {
        if (isRefreshing) {
            mShopCategoryGoodsView.refreshContent(
                    mShopCategoryGoodsView.checkResultObject(goodsGridList));
        }

        mGoodsList = goodsList;
        mGoodsGridList = goodsGridList;

        mEmptyResult = emptyResult;
        mHasMoreData = hasMoreData;

        refreshStyleUI(isGridStyle);
    }

    @Override
    public void loadCategoryPriceGoodsList(final boolean isRefreshing, boolean isSortUp) {

        if (isRefreshing) {
            mShopCategoryGoodsView.showLoadingDialog();
        }

        mShopModel.loadCategoryPriceGoodsList(
                isRefreshing,
                supplierId,
                categoryId,
                isSortUp,
                mShopCategoryGoodsView.getPagerTag(),
                new IShopModel.OnLoadShopGoodsListener() {
                    @Override
                    public void onSuccess(
                            List<Goods> goodsList,
                            List<DoubleGoods> goodsGridList,
                            boolean emptyResult,
                            boolean hasMoreData) {
                        handleResponse(
                                goodsList, goodsGridList, emptyResult, hasMoreData, isRefreshing);
                    }

                    @Override
                    public void onFailed() {
                        if (isRefreshing) {
                            mShopCategoryGoodsView.refreshContent(
                                    SuperLoadingLayout.STATE_ERROR);
                        } else {
                            mShopCategoryGoodsView.loadMoreError();
                        }
                    }

                    @Override
                    public void onFinal() {
                        mShopCategoryGoodsView.hideLoadingDialog();
                    }
                });
    }

    @Override
    public void loadCategorySalesGoodsList(final boolean isRefreshing) {

        if (isRefreshing) {
            mShopCategoryGoodsView.showLoadingDialog();
        }

        mShopModel.loadCategorySalesGoodsList(
                isRefreshing,
                supplierId,
                categoryId,
                mShopCategoryGoodsView.getPagerTag(),
                new IShopModel.OnLoadShopGoodsListener() {
                    @Override
                    public void onSuccess(
                            List<Goods> goodsList,
                            List<DoubleGoods> goodsGridList,
                            boolean emptyResult,
                            boolean hasMoreData) {
                        handleResponse(
                                goodsList, goodsGridList, emptyResult, hasMoreData, isRefreshing);
                    }

                    @Override
                    public void onFailed() {
                        if (isRefreshing) {
                            mShopCategoryGoodsView.refreshContent(
                                    SuperLoadingLayout.STATE_ERROR);
                        } else {
                            mShopCategoryGoodsView.loadMoreError();
                        }
                    }

                    @Override
                    public void onFinal() {
                        mShopCategoryGoodsView.hideLoadingDialog();
                    }
                });
    }

    @Override
    public void changeStyle(boolean isGridStyle) {
        this.isGridStyle = isGridStyle;
        refreshStyleUI(isGridStyle);
    }

    private void refreshStyleUI(boolean isGridStyle) {
        if (isGridStyle) {
            // 当前是网格状
            mShopCategoryGoodsView.setGoodsList(mGoodsList);
        } else {
            mShopCategoryGoodsView.setGoodsGrid(mGoodsGridList);
        }
        refreshLoadMoreState();
    }

    private void refreshLoadMoreState() {
        mShopCategoryGoodsView.loadMoreFinish(mEmptyResult, mHasMoreData);
    }
}
